﻿using de.wolfmarco.pas.Shared.MessageBoxes;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace de.wolfmarco.pas.Shared.MVVM
{
    public class EditWindowViewModel : ViewModelBase
    {
        public EditWindowViewModel()
            : base()
        {
            SaveCommand = new DelegateCommand<object>(OnTrySave, CanSave);
            SaveAndCloseCommand = new DelegateCommand<object>(OnTrySaveAndClose, CanSaveAndClose);
            DeleteCommand = new DelegateCommand<object>(OnTryDelete, CanDelete);
        }

        #region SaveCommand

        public DelegateCommand<object> SaveCommand { get; private set; }

        private void OnTrySave(object arg)
        {
            try
            {
                OnSave(arg);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual void OnSave(object arg)
        {
        }

        protected virtual bool CanSave(object arg)
        {
            return true;
        }

        #endregion SaveCommand

        #region SaveAndCloseCommand

        public DelegateCommand<object> SaveAndCloseCommand { get; private set; }

        private void OnTrySaveAndClose(object arg)
        {
            try
            {
                OnSaveAndClose(arg);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual void OnSaveAndClose(object arg)
        {
        }

        protected virtual bool CanSaveAndClose(object arg)
        {
            return true;
        }

        #endregion SaveCommand

        #region DeleteCommand

        public DelegateCommand<object> DeleteCommand { get; private set; }

        private void OnTryDelete(object arg)
        {
            try
            {
                OnDelete(arg);
            }
            catch (Exception ex)
            {
                PASMessageBox.ShowException(ex);
            }
        }

        protected virtual void OnDelete(object arg)
        {
        }

        protected virtual bool CanDelete(object arg)
        {
            return true;
        }

        #endregion DeleteCommand
    }
}
